﻿using QuanLyThuVien.DAL;
using QuanLyThuVien.DTO;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuanLyThuVien.BLL
{
    //----------------------------------------------------------------------
    //Desc: class xử lý cho bảng TACGIA
    //----------------------------------------------------------------------
    public class BLLTacGia
    {
        //----------------------------------------------------------------------
        //Desc: constructor mặc định
        //----------------------------------------------------------------------
        public BLLTacGia()
        { }

        //----------------------------------------------------------------------
        //Desc: lấy danh sách tác giả
        //----------------------------------------------------------------------
        public List<DTOTacGia> LayDSTacGia()
        {
            DALTacGia _dalTacGia = new DALTacGia();
            return _dalTacGia.LayDSTacGia();
        }

        //----------------------------------------------------------------------
        //Desc: lấy thông tin tác giả
        //----------------------------------------------------------------------
        public DTOTacGia LayThongTinTacGia(string matg)
        {
            DALTacGia _dalTacGia = new DALTacGia();
            return _dalTacGia.LayThongTinTacGia(matg);
        }

        //----------------------------------------------------------------------
        //Desc: lấy danh sách tác giả, trả về datatable
        //----------------------------------------------------------------------
        public DataTable LayDanhSachTacGia()
        {
            DALTacGia _dalTacGia = new DALTacGia();
            return _dalTacGia.LayDanhSachTacGia();
        }

        //----------------------------------------------------------------------
        //Desc: lấy tên tác giả
        //----------------------------------------------------------------------
        public string LayTenTG(string matg)
        {
            DALTacGia _dalTacGia = new DALTacGia();
            return _dalTacGia.LayTenTG(matg);
        }

        //----------------------------------------------------------------------
        //Desc: phát sinh mã tác giả
        //----------------------------------------------------------------------
        public bool PhatSinhMaTacGia(ref DTOTacGia tacgia)
        {
            DALTacGia _dalTacGia = new DALTacGia();
            int stt = _dalTacGia.DemTacGia();
            if (stt < 0)
                return false;
            if (stt == 0)
                stt++;
            string matacgia = "TG" + stt.ToString("D3");
            int dem = _dalTacGia.DemMaTacGia(matacgia);
            while (dem > 0)
            {
                stt++;
                matacgia = "TG" + stt.ToString("D3");
                dem = _dalTacGia.DemMaTacGia(matacgia);
            }

            if (dem < 0)
                return false;

            tacgia.MaTG = matacgia;
            return true;
        }

        //----------------------------------------------------------------------
        //Desc: cập nhật tác giả
        //----------------------------------------------------------------------
        public bool CapNhat(DTOTacGia tacgia)
        {
            DALTacGia _dalTacGia = new DALTacGia();
            return _dalTacGia.CapNhat(tacgia);
        }

        //----------------------------------------------------------------------
        //Desc: kiểm tra tồn tại đầu sách
        //----------------------------------------------------------------------
        public bool KiemTraTonTaiDauSach(string matacgia)
        {
            DALTacGia _dalTacGia = new DALTacGia();
            int dem = _dalTacGia.DemSoDauSachCuaTG(matacgia);
            if (dem != 0)
                return true;
            else
                return false;
        }

        //----------------------------------------------------------------------
        //Desc: xoá tác giả
        //----------------------------------------------------------------------
        public bool Xoa(DTOTacGia tacgia)
        {
            DALTacGia _dalTacGia = new DALTacGia();
            return _dalTacGia.Xoa(tacgia);
        }
    }
}
